//
//  AppDelegate.m
//  pyq
//
//  Created by dev-oo on 2017/10/19.
//  Copyright © 2017年 pingmei. All rights reserved.
//

#import "AppDelegate.h"
#import "MainVC.h"
#import <ShareSDK/ShareSDK.h>
#import <ShareSDKConnector/ShareSDKConnector.h>
//腾讯开放平台（对应QQ和QQ空间）SDK头文件
#import <TencentOpenAPI/TencentOAuth.h>
#import <TencentOpenAPI/QQApiInterface.h>
//微信SDK头文件
#import "WXApi.h"
//新浪微博SDK头文件
#import "WeiboSDK.h"

@interface AppDelegate ()

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    CGRect rcScreen = [[UIScreen mainScreen] bounds];
    self.window = [[UIWindow alloc] initWithFrame:rcScreen];
    self.window.backgroundColor = [UIColor whiteColor];
    self.window.rootViewController = [[UINavigationController alloc] initWithRootViewController:[[MainVC alloc]init]];
    [self.window makeKeyAndVisible];
    
    /**初始化ShareSDK应用
     @param activePlatforms
     使用的分享平台集合
     @param importHandler (onImport)
     导入回调处理，当某个平台的功能需要依赖原平台提供的SDK支持时，需要在此方法中对原平台SDK进行导入操作
     @param configurationHandler (onConfiguration)
     配置回调处理，在此方法中根据设置的platformType来填充应用配置信息
     */
    [ShareSDK registerActivePlatforms:@[
                                        @(SSDKPlatformTypeSinaWeibo),
                                        @(SSDKPlatformTypeWechat),
                                        @(SSDKPlatformTypeQQ),
                                        ]
                             onImport:^(SSDKPlatformType platformType)
     {
         switch (platformType)
         {
             case SSDKPlatformTypeWechat:
                 [ShareSDKConnector connectWeChat:[WXApi class]];
                 break;
             case SSDKPlatformTypeQQ:
                 [ShareSDKConnector connectQQ:[QQApiInterface class] tencentOAuthClass:[TencentOAuth class]];
                 break;
             case SSDKPlatformTypeSinaWeibo:
                 [ShareSDKConnector connectWeibo:[WeiboSDK class]];
                 break;
             default:
                 break;
         }
     }
          onConfiguration:^(SSDKPlatformType platformType, NSMutableDictionary *appInfo)
     {
         switch (platformType)
         {
             case SSDKPlatformTypeSinaWeibo:
                 //设置新浪微博应用信息,其中authType设置为使用SSO＋Web形式授权
                 [appInfo SSDKSetupSinaWeiboByAppKey:@"2017192464"
                                           appSecret:@"027759814e45f26fa6bffd13903db906"
                                         redirectUri:@"http://www.sharesdk.cn"
                                            authType:SSDKAuthTypeBoth];
                 break;
             case SSDKPlatformTypeWechat:
                 [appInfo SSDKSetupWeChatByAppId:@"wx94a39d3edb2c0e50"
                                       appSecret:@"6bbccfab7034846952e83c11e7dd340e"];
                 break;
             case SSDKPlatformTypeQQ:
                 [appInfo SSDKSetupQQByAppId:@"1106412987"
                                      appKey:@"DlrBbqkOpqOPNmsX"
                                    authType:SSDKAuthTypeBoth];
                 break;
             default:
                   break;
                   }
                   }];
    
    [WXApi registerApp:@"wx94a39d3edb2c0e50"];
    
    return YES;
}

-(BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url{
    NSLog(@"handleOpenURL %@", url);
    return [WXApi handleOpenURL:url delegate:self];
}

-(BOOL) application:(UIApplication *)app openURL:(NSURL *)url sourceApplication:(nullable NSString *)sourceApplication annotation:(nonnull id)annotation{
    NSLog(@"openURL   sourceApplication %@", url);
    return [WXApi handleOpenURL:url delegate:self];
}

// NOTE: 9.0以后使用新API接口
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString*, id> *)options{
    NSLog(@"openURL  options %@", url);
    return [WXApi handleOpenURL:url delegate:self];
}

-(void)onResp:(BaseResp*)resp{
    if ([resp isKindOfClass:[PayResp class]]){
        PayResp *response=(PayResp*)resp;
        switch(response.errCode){
            case WXSuccess:{
                NSLog(@"微信支付成功");
                NSString *resultCode = [NSString stringWithFormat:@"%d", response.errCode];
                [[NSNotificationCenter defaultCenter] postNotificationName:@"WXPaySuceess" object:resultCode];
            }
                break;
            default:
            {
                NSString *resultCode = [NSString stringWithFormat:@"%d", response.errCode];
                NSLog(@"微信支付失败，retcode=%d",resp.errCode);
                [[NSNotificationCenter defaultCenter] postNotificationName:@"WXPayFail" object:resultCode];
            }
                break;
        }
    }else if ([resp isKindOfClass:[SendAuthResp class]]) {        // 向微信请求授权后,得到响应结果
        SendAuthResp *temp = (SendAuthResp *)resp;
        [[NSNotificationCenter defaultCenter] postNotificationName:@"getWxAccessToken" object:temp.code];
    }
}

- (void)applicationWillResignActive:(UIApplication *)application {
}


- (void)applicationDidEnterBackground:(UIApplication *)application {
}


- (void)applicationWillEnterForeground:(UIApplication *)application {
}


- (void)applicationDidBecomeActive:(UIApplication *)application {
}


- (void)applicationWillTerminate:(UIApplication *)application {
}


@end
